package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.mapper.ISupplierMapper;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商
 */
@Service
@Transactional
public class SupplierService implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;

    @Override
    public IPage<SupplierBean> cutByItem(Integer pageNO, String name, String scope) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        if (name != null && !name.equals("")) {
            qw.like("s_name",name);
        }
        if(scope!=null&&!scope.equals("")){
            qw.like("s_scope",scope);
        }
        return supplierMapper.selectPage(new Page<>(pageNO,2),qw);
    }

    @Override
    public SupplierBean findById(Integer id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String newHead, String newPhone) {
        SupplierBean supplierBean = supplierMapper.selectById(id);
        supplierBean.setHead(newHead);
        supplierBean.setPhone(newPhone);
        supplierMapper.updateById(supplierBean);
    }

    @Override
    public SupplierBean findSupplier(String name) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        qw.eq("s_name",name);
        return supplierMapper.selectOne(qw);
    }

    @Override
    public List<SupplierBean> findSupplierList() {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        return supplierMapper.selectList(qw);
    }

    @Override
    public void add(SupplierBean supplierBean) {
            supplierMapper.insert(supplierBean);
    }
}
